package com.meeruu.sg.account.core.domain.facade.impl;

import com.meeruu.common.model.enums.SysConfigCodeEnum;
import com.meeruu.common.model.error.BaseCode;
import com.meeruu.common.model.result.BaseRpcResult;
import com.meeruu.common.utils.mapper.OrikaMapperUtils;
import com.meeruu.sg.account.core.api.dto.response.SysCoreConfigDTO;
import com.meeruu.sg.account.core.api.service.SysCoreConfigService;
import com.meeruu.sg.account.core.domain.biz.SysCoreConfigBiz;
import com.meeruu.sg.account.core.domain.model.SysCoreConfig;
import com.meeruu.sg.account.core.domain.model.enums.StatusEnums;
import com.meeruu.summerframework.cloud.anno.MRSCProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jiangdawei on 2018/9/4 13:54.
 */
@MRSCProvider(serviceInterface = SysCoreConfigService.class)
@Component
public class SysCoreConfigServiceImpl implements SysCoreConfigService {

    @Autowired
    private SysCoreConfigBiz sysCoreConfigBiz;

    @Override
    public BaseRpcResult<List<SysCoreConfigDTO>> queryWithdrawConfig() {
        List<SysCoreConfig> configs = sysCoreConfigBiz.queryConfigs(Arrays.asList(SysConfigCodeEnum.SERVICE_CHARGE.getCode(),
                SysConfigCodeEnum.MIN_BALANCE.getCode(),
                SysConfigCodeEnum.WHEN_LESS_THAN_BALANCE.getCode(),
                SysConfigCodeEnum.CASE_SERVICE_CHARGE.getCode()), null);

        configs = configs.stream().filter(x -> StatusEnums.ENABLE.getCode().equals(x.getStatus())).collect(Collectors.toList());
        return new BaseRpcResult<>(OrikaMapperUtils.mapList(configs,
                OrikaMapperUtils.getType(SysCoreConfig.class), OrikaMapperUtils.getType(SysCoreConfigDTO.class)));
    }

    @Override
    public BaseRpcResult<SysCoreConfigDTO> queryByCode(String code) {
        SysCoreConfig sysCoreConfig = sysCoreConfigBiz.queryByCode(code);
        if (sysCoreConfig == null) {
            return new BaseRpcResult<>(null);
        }
        SysCoreConfigDTO dto = OrikaMapperUtils.map(sysCoreConfig, OrikaMapperUtils.getType(SysCoreConfig.class),
                OrikaMapperUtils.getType(SysCoreConfigDTO.class));
        return new BaseRpcResult<>(dto);
    }


    @Override
    public BaseRpcResult<List<SysCoreConfigDTO>> queryConfigList(List<String> codes) {
        if (CollectionUtils.isEmpty(codes)) {
            return new BaseRpcResult<List<SysCoreConfigDTO>>().error(BaseCode.PARAM_ERROR);
        }
        List<SysCoreConfig> sysCoreConfig = sysCoreConfigBiz.queryConfigList(codes);

        return new BaseRpcResult<>(OrikaMapperUtils.mapList(sysCoreConfig,
                OrikaMapperUtils.getType(SysCoreConfig.class), OrikaMapperUtils.getType(SysCoreConfigDTO.class)));
    }

    @Override
    public BaseRpcResult<Void> addConfigList(List<SysCoreConfigDTO> configDTOS) {
        List<SysCoreConfig> sysCoreConfigs = OrikaMapperUtils.mapList(configDTOS, SysCoreConfigDTO.class, SysCoreConfig.class);
        sysCoreConfigBiz.addConfigList(sysCoreConfigs);
        return new BaseRpcResult<>();
    }

    @Override
    public BaseRpcResult<Void> saveWithdrawConfigList(List<SysCoreConfigDTO> configDTOS) {
        List<SysCoreConfig> sysCoreConfigs = OrikaMapperUtils.mapList(configDTOS, SysCoreConfigDTO.class, SysCoreConfig.class);
        sysCoreConfigBiz.saveWithdrawConfigList(sysCoreConfigs);
        return new BaseRpcResult<>();
    }

    @Override
    public BaseRpcResult<List<SysCoreConfigDTO>> queryConfigs(List<String> codes, Date time) {
        List<SysCoreConfig> configs = sysCoreConfigBiz.queryConfigs(codes, time);

        return new BaseRpcResult<>(OrikaMapperUtils.mapList(configs,
                OrikaMapperUtils.getType(SysCoreConfig.class), OrikaMapperUtils.getType(SysCoreConfigDTO.class)));
    }

    @Override
    public BaseRpcResult<SysCoreConfigDTO> queryByTime(String code, Date time) {
        SysCoreConfig sysCoreConfig = sysCoreConfigBiz.queryByTime(code, time);
        return new BaseRpcResult<>(OrikaMapperUtils.map(sysCoreConfig, SysCoreConfigDTO.class));
    }

    /**
     * 查询资金中心配置
     * @param codes
     * @return
     */
    public BaseRpcResult<List<SysCoreConfigDTO>> queryFundManagerWithdrawConfig(List<String> codes) {
        List<SysCoreConfig> sysCoreConfigList = sysCoreConfigBiz.queryWithdrawConfig(codes);
        return new BaseRpcResult<>(OrikaMapperUtils.mapList(sysCoreConfigList, SysCoreConfig.class, SysCoreConfigDTO.class));
    }


}
